import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Store from '../store' 

Vue.use(VueRouter)
Vue.use(Store)

const routes = [{
    path: "/",
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import( /* webpackChunkName: "goods" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'User',
    component: () => import( /* webpackChunkName: "user" */ '../views/User.vue')
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import( /* webpackChunkName: "order" */ '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import( /* webpackChunkName: "free" */ '../views/Free.vue')
  },
  {
    path: '/details/:id',
    name: 'Details',
    component: () => import( /* webpackChunkName: "details" */ '../views/Details.vue'),
   
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import( /* webpackChunkName: "details" */ '../components/Cart.vue')
  }
 
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/user') {
    // 当用户要跳转路由到 /user 时，判断是否登录
    // 必须有token
    let token = localStorage.getItem('x-auth-token')
    if(token){
      next()
    }else{
      // 给提示
      let obj = {content : "请先登录!",icon : "info"}
      Store.dispatch('AsyncToast',obj)
    }
    return
  }
  next()
})

export default router